#include "widget.h"
#include "ui_widget.h"
QMap<QString, QTcpSocket*> clientMap; // 存放用户socket的静态成员变量

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    server = new QTcpServer;
    // 监听本地的 IP 地址和端口
    if (!server->listen(QHostAddress::AnyIPv4, 1234)) {
        qDebug() << "Failed to start the server!";
        return;
    }

    qDebug() << "Server started!";

    // 设计表格的样式
    ui->tableWidget->setColumnWidth(0, 100);
    ui->tableWidget->setColumnWidth(1, 100);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);

    // 连接新连接的信号与槽函数, 处理连接后的操作
    connect(server, &QTcpServer::newConnection, this, [=]() {
        // 获取与客户端的连接套接字
        QTcpSocket *clientSocket = server->nextPendingConnection();

        // 客户端“断开连接”时连接槽函数， 删除表格中的对应内容
        connect(clientSocket, &QTcpSocket::disconnected, this, [this, clientSocket]() {
            // 获取断开连接的客户端的端口和IP
            quint16 clientPort = clientSocket->peerPort();
            QString clientIP = clientSocket->peerAddress().toString();

            // 在表格中删除对应的行
            for (int row = 0; row < ui->tableWidget->rowCount(); ++row) {
                QTableWidgetItem* portItem = ui->tableWidget->item(row, 1); // 端口列
                QTableWidgetItem* ipItem = ui->tableWidget->item(row, 2); // IP列
                if (portItem && ipItem && portItem->text().toInt() == clientPort && ipItem->text() == clientIP) {
                    ui->tableWidget->removeRow(row);
                    break;
                }
            }
        });

        // 获取客户端的 IP 地址和端口
        QString clientIP = clientSocket->peerAddress().toString();
        quint16 clientPort = clientSocket->peerPort();

        // 将 IP 地址和端口分别写入 UI 控件

        int row = ui->tableWidget->rowCount();
        qDebug() << "row 52 in widget.cpp, row is"<< row;
        QString clientName = "客户端" + QString::number(row + 1);
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row, 0, new QTableWidgetItem(clientName));
        ui->tableWidget->setItem(row, 1, new QTableWidgetItem(QString::number(clientPort)));
        ui->tableWidget->setItem(row, 2, new QTableWidgetItem(clientIP));

        // 新建ClientThread类去处理一个连接的客户端的接收消息
        ClientThread *thread = new ClientThread(clientSocket, row);
        connect(thread, &ClientThread::returnName, this, [this, &clientSocket](QString clientName, int row) {
            ui->tableWidget->setItem(row, 0, new QTableWidgetItem(clientName));
//            clientMap.insert(clientName, clientSocket);
        });
        thread->start();

        // 打印QMap值
//        // 使用迭代器遍历 QMap
//        QMap<QString, QTcpSocket*>::const_iterator it;
//        for (it = clientMap.constBegin(); it != clientMap.constEnd(); ++it) {
//            QString key = it.key();
//            QTcpSocket* socket = it.value();
//            qDebug() << "Key:" << key << ", Socket:" << socket;
//        }

    });


}

Widget::~Widget()
{
    delete ui;
}

